﻿/**********************************************
 * 功能名称：操作员管理
编写人：  杨莹
建立时间： 杨 2008年11月19日
说明：

 *列表显示操作员，可对操作员信息进行增删改。
 * 
 * 
 **************************************************************/

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Hisun.Controllers;
using Hisun.Models;
using Hisun.UserControl;
using System.Text;

namespace Hisun.View.Management
{
    public partial class UserManagement : AbcPageBase
    {
        HisunDBDataContext db = new HisunDBDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                BindMyData(0);

            }
        }

        #region 绑定数据及翻页
        private void BindMyData(int PageIndex)
        {

            int skipAccount = PageIndex * 20;

            var MyQuery = (from a in db.TAbc_JS_Users select a).Skip(skipAccount).Take(20);
            GridView_Users.DataSource = MyQuery;
            GridView_Users.DataBind();

        }

        //查询出当前记录信息的总条数。
        private int TotalPageAmount()
        {
            int MyQuery = 0;
            MyQuery = (from a in db.TAbc_JS_Users select a).Count();
            return MyQuery;
        }


        //点击按钮，执行查询
        protected void MyClick(object sender, EventArgs e)
        {


            int pageIndx = Convert.ToInt32(CurrentPage.Value);
            int Totals = TotalPageAmount();



            int pages = (Totals % 20) == 0 ? (Totals / 20) : (Totals / 20 + 1);

            Label_Total.Text = "共" + pages.ToString();

            if (typeof(Button).IsInstanceOfType(sender))
            {
                string arg = ((Button)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "first":

                        pageIndx = 0;

                        break;
                    case "gotopage":
                        pageIndx = Convert.ToInt32(TextBox_PageIndex.Text) - 1;
                        break;
                }

            }
            else if (typeof(LinkButton).IsInstanceOfType(sender))
            {

                string arg = ((LinkButton)sender).CommandArgument.ToString().ToLower();

                switch (arg)
                {
                    case "prev":
                        if (pageIndx > 0)
                        {
                            pageIndx = pageIndx - 1;
                        }
                        break;
                    case "next":
                        if (pageIndx < pages - 1)
                        {
                            pageIndx = pageIndx + 1;
                        }
                        break;
                    case "last":
                        pageIndx = pages - 1;
                        break;
                    case "first":
                        pageIndx = 0;
                        break;
                }

            }

            CurrentPage.Value = pageIndx.ToString();  //对保存页码状态信息的隐藏字段进行赋值。保存当前页码。
            Label_CurrenPageAndTo.Text = "页，当前第" + (pageIndx + 1).ToString() + " 页，转到";

            BindMyData(pageIndx);

        }

        #endregion


        //增加按钮的事件
        protected void Button_Add_Command(object sender, CommandEventArgs e)
        {
            Panel_Gridlist.Visible = false;

            TextBox_CenterId.Text = "";
            TextBox_CenterName.Text = "";
            TextBox_UserId.Text = "";
            TextBox_UserName.Text = "";
            CheckBox_StopFlag.Checked = false;

            Panel_Edit.Visible = true;

            EditFlag.Value = "add";




        }
        //保存的按钮
        protected void Button_Save_Command(object sender, CommandEventArgs e)
        {

            if (EditFlag.Value == "add")
            {


                TAbc_JS_Users myrow = new TAbc_JS_Users();
                myrow.操作员ID = TextBox_UserId.Text;
                myrow.操作员姓名 = TextBox_UserName.Text;
                myrow.责任部门ID = Convert.ToInt32(TextBox_CenterId.Text);
                myrow.停用 = CheckBox_StopFlag.Checked;
                myrow.公司ID = "hisun";
                myrow.创建时间 = DateTime.Now;
                myrow.编辑人ID = Session[ServerVars.sysSessionUserName].ToString();
                myrow.权限ID = "";
                myrow.角色ID = "";
                myrow.拥有权限ID = "";
                myrow.密码 = FormsAuthentication.HashPasswordForStoringInConfigFile("123456", "MD5");
                db.TAbc_JS_Users.InsertOnSubmit(myrow);


                try
                {
                    db.SubmitChanges();
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('增加成功'); </script>");

                    EditFlag.Value = "0";
                    Panel_Edit.Visible = false;
                    Panel_Gridlist.Visible = true;
                    BindMyData(0);
                }
                catch
                {
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('增加失败'); </script>");
                }


            }
            else if (EditFlag.Value == "edit")
            {
                string myUserId = TextBox_UserId.Text;
                var myEditQuery = (from a in db.TAbc_JS_Users where a.操作员ID == myUserId select a).Single();

                myEditQuery.操作员姓名 = TextBox_UserName.Text;
                myEditQuery.责任部门ID = Convert.ToInt32(TextBox_CenterId.Text);
                myEditQuery.停用 = CheckBox_StopFlag.Checked;
                myEditQuery.编辑人ID = Session[ServerVars.sysSessionUserName].ToString();
                myEditQuery.修改时间 = DateTime.Now;

                try
                {
                    db.SubmitChanges();
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('编辑成功'); </script>");

                    EditFlag.Value = "0";
                    Panel_Edit.Visible = false;
                    Panel_Gridlist.Visible = true;
                    TextBox_UserId.Enabled = true;
                    BindMyData(0);

                }
                catch
                {
                    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "message", " <script>alert('编辑失败');</script>");
                }

            }

        }
        //取消的按钮
        protected void Button_Cancel_Command(object sender, CommandEventArgs e)
        {
            EditFlag.Value = "0";
            Panel_Edit.Visible = false;
            Panel_Gridlist.Visible = true;
            if (EditFlag.Value == "edit")
            {
                TextBox_UserId.Enabled = true;

            }

        }

        protected void Button_Edit_Command(object sender, CommandEventArgs e)
        {
            string myid = e.CommandArgument.ToString();
            var myEditQuery = (from a in db.TAbc_JS_Users
                               join b in db.TABC_JS_CostCenter on a.责任部门ID equals b.作业中心ID into myuser
                               where a.操作员ID == myid
                               from user in myuser.DefaultIfEmpty()
                               select new
                               {
                                   a.操作员ID,
                                   a.操作员姓名,
                                   a.责任部门ID,
                                   a.停用,
                                   作业中心名称 = (user == null ? "" : user.作业中心名称)

                               }
               
                //这个语句可以防止责任部门为空的时候，查询报错。


                               ).Single();
            TextBox_CenterId.Text = myEditQuery.责任部门ID.ToString();
            TextBox_CenterName.Text = myEditQuery.作业中心名称;
            TextBox_UserId.Text = myEditQuery.操作员ID;
            TextBox_UserName.Text = myEditQuery.操作员姓名;
            CheckBox_StopFlag.Checked = myEditQuery.停用;


            TextBox_UserId.Enabled = false;
            EditFlag.Value = "edit";
            Panel_Edit.Visible = true;
            Panel_Gridlist.Visible = false;

        }

        protected void Button_SelectCenter_Command(object sender, CommandEventArgs e)
        {
            StringBuilder s = new StringBuilder();
            s.Append("<script language=javascript>");
            s.Append("var a=window.showModalDialog('UserManagementSelect.aspx');");
            s.Append("if(a!=null)");
            s.Append(@"{ var aa =a.split(""&"") ;");
            s.Append("document.all('TextBox_CenterId').value=aa[0];");
            s.Append("document.all('TextBox_CenterName').value=aa[1];}");

            s.Append("</script>");
            Type cstype = this.GetType();
            ClientScriptManager cs = Page.ClientScript;
            string sname = "lt";
            if (!cs.IsStartupScriptRegistered(cstype, sname))
            { cs.RegisterStartupScript(cstype, sname, s.ToString()); }
        }







    }
}
